let channel = ''
// #ifdef APP-PLUS
channel = 'APP';
// #endif
// #ifdef MP-WEIXIN
channel = 'WEIXIN';
// #endif
// #ifdef H5
channel = 'H5';
// #endif
uni.setStorageSync('channel', channel); //当前平台
// 统一请求
const instance = async (api, options) => {
	// 时分秒获取
	// let now = new Date();
	// let year = now.getFullYear(); //得到年份
	// let month = now.getMonth(); //得到月份
	// let date = now.getDate(); //得到日期
	// let day = now.getDay(); //得到周几
	// let hour = now.getHours(); //得到小时
	// let minu = now.getMinutes(); //得到分钟
	// let sec = now.getSeconds(); //得到秒
	// let MS = now.getMilliseconds(); //获取毫秒
	// let w_Date = minu + "分" + sec + "秒" + MS



	let api_url = 'https://sp.6-7-8-9.com/app1' + api; //不使用代理匹配
	if (channel == 'H5') {
		api_url = '/api123' + api; //使用代理匹配替换-在manifest.json 文件的h5.devServer.proxy里做匹配
	}
	// 如果传参没有http字符,使用旧版接口链接
	// if (api_url.indexOf('http') === -1) api_url = baseURL + api_url + "
	// return;

	let header = {
		'Authorization': uni.getStorageSync('login@token') || '',
		'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
		'VS-PHPSESSID': uni.getStorageSync('VS_PHPSESSID') || '',
		'Login-Token': uni.getStorageSync('login@token') || '',
		// 'cookie': 'PHPSESSID=upe3prmfd8i8sth99kelqdqn3e',
	}
	if (channel) header.Channel = channel
	if (options.header) header = {
		...header,
		...options.header
	}
	options.method = options.method || 'GET'
	if (options.method == 'POST') {
		uni.showLoading({
			title: '请稍等',
			mask: true
		});
	}

	let [error, res] = await uni.request({
		url: api_url,
		header: header,
		method: options.method,
		data: options.data,
	})
	// console.log("========HTTP回调-api_url===========", api_url);
	// console.log("========HTTP回调-res===========", res);
	// console.log("========HTTP回调-error===========", error);

	uni.hideLoading()
	if (res == undefined) {
		uni.showToast({
			title: '网络错误[' + res + ']',
			// mask: true,//是否显示透明蒙层，防止触摸穿透，默认：false
			icon: "none",
			duration: 3000,
		});
		return
	}
	if (error == "request:fail") {
		uni.showToast({
			title: '网络错误[' + error + ']',
			// mask: true,//是否显示透明蒙层，防止触摸穿透，默认：false
			icon: "none",
			duration: 3000,
		});
		return
	}
	if (res.statusCode != 200) {
		uni.showToast({
			title: '网络异常[' + res.statusCode + ']',
			// mask: true,//是否显示透明蒙层，防止触摸穿透，默认：false
			icon: "none",
			duration: 3000,
		});
		return
	}
	if (res.data.code == 909) {
		uni.showModal({
			title: '提示',
			content: '未登录' + res.data.msg,
			success: function(res) {
				if (res.confirm) {
					console.log('用户点击确定');
					// 转到登录页面
					uni.navigateTo({
						url: '/pages/authorize/login',
						animationDuration: 300,
						animationType: 'slide-in-bottom'
					})
				} else if (res.cancel) {
					// console.log('用户点击取消');
					uni.navigateBack();
					return
				}
			}
		});
	}

	if (res.data.code != 200) {
		uni.showToast({
			title: res.data.msg ? res.data.msg : "请求数据失败",
			// mask: true,//是否显示透明蒙层，防止触摸穿透，默认：false
			icon: "none",
			duration: 3000,
		});
		return
	}
	// 微信环境
	if (channel == 'WEIXIN') {
		uni.hideLoading()
		if (res.statusCode != 200) {
			uni.showToast({
				title: '网络异常[' + res.statusCode + ']',
				// mask: true,//是否显示透明蒙层，防止触摸穿透，默认：false
				icon: "none",
				duration: 3000,
			});
			return
		}


		// if (res.header.VS_PHPSESSID) {
		// 	console.log("========设置同步本地缓存===========");
		// 	uni.setStorageSync('VS_PHPSESSID', res.header.VS_PHPSESSID);
		// }

	}
	return res
}

const http = {

	get: async (api_url, params, config) => {
		console.log("get请求");
		let options = {};
		if (typeof config === 'undefined') config = {}
		options = {
			...options,
			...config
		}
		if (typeof params !== 'undefined') options.data = params;
		options.method = 'GET';
		// 解决nvue不渲染问题
		return await new Promise((resolve, reject) => {
			instance(api_url, options).then((res) => {
				setTimeout(() => {
					resolve(res)
				}, 1)
			})
		})
	},


	post: async (api_url, params) => {
		// console.log("post请求");
		let options = {};
		if (typeof params !== 'undefined') options.data = params;
		options.method = 'POST';
		return await new Promise((resolve, reject) => {
			instance(api_url, options).then((res) => {
				setTimeout(() => {
					resolve(res)
				}, 1)
			})
		})
	},

	delete: async (api_url, params) => {
		let options = {};
		if (typeof params !== 'undefined') options.data = params;
		options.method = 'DELETE';
		return await new Promise((resolve, reject) => {
			instance(api_url, options).then((res) => {
				setTimeout(() => {
					resolve(res)
				}, 1)
			})
		})
	},
}
module.exports = http